{% extends '../_manage.html' %}

{% block title %} {{ _('Settings') }} {% endblock %}

{% block head %}

<script id="field-text" type="text/plain">
    <div class="uk-form-row">
        <label class="uk-form-label">{ label }:</label>
        <div class="uk-form-controls">
            <input v-model="{ key }" type="text" maxlength="100" class="uk-width-1-1" placeholder="{ description }">
        </div>
    </div>
</script>

<script id="field-textarea" type="text/plain">
    <div class="uk-form-row">
        <label class="uk-form-label">{ label }:</label>
        <div class="uk-form-controls">
            <textarea v-model="{ key }" class="uk-width-1-1" rows="16" placeholder="{ description }" style="resize:none;"></textarea>
        </div>
    </div>
</script>

<script>
var GROUP = '{{ group }}';

$(function() {
    getJSON('/api/settings/definitions', function (err, defs) {
        if (err) {
            return fatal(err);
        }
        getJSON('/api/settings/' + GROUP, function (err, settings) {
            if (err) {
                return fatal(err);
            }
            initVM(defs[GROUP], settings);
        });
    });
});

function initVM(definitions, settings) {
    var templates = {
        text: new Template($('#field-text').html()),
        textarea: new Template($('#field-textarea').html())
    };
    var fields = [];
    for (var i=0; i<definitions.length; i++) {
        var definition = definitions[i];
        var html = templates[definition.type].render(definition);
        fields.push(html);
    }
    $('#vm form .x-replace').after(fields.join(''));
    var vm = new Vue({
        el: '#vm',
        data: settings,
        methods: {
            submit: function(event) {
                event.preventDefault();
                $('#vm form .uk-alert-success').hide();
                $('#vm').find('form').postJSON('{{ form.action }}', this.$data, function(err, result) {
                    if (err) {
                        return;
                    }
                    $('#vm form .uk-alert-success').show();
                    location.assign('#');
                    return true;
                });
            }
        }
    });
    $('#loading').hide();
    $('#vm').show();
    g=vm;
}
</script>
{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> Loading...
    </div>

    <div id="vm" class="uk-width-1-1">
        <ul class="uk-tab" data-uk-tab>
            {% for t in tabs %}
            {% if t.key === group %}
            <li class="uk-active"><a href="#0">{{ t.name }}</a></li>
            {% else %}
            <li><a href="#0" onclick="location.assign('{{ t.key }}')">{{ t.name }}</a></li>
            {% endif %}
            {% endfor %}
        </ul>
        <form v-on="submit: submit" class="uk-form uk-form-stacked uk-margin">
            <legend>{{ form.name }}</legend>
            <fieldset>
                <div class="uk-alert uk-alert-success" style="display:none">
                    Settings saved.
                </div>
                <div class="uk-alert uk-alert-danger uk-hidden"></div>
                <div class="x-replace"></div>
                <div class="uk-form-row">
                    <div class="controls">
                        <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-check"></i> Save</button>
                        <button type="button" class="uk-button" onclick="location.assign('{{ form.redirect }}')"><i class="uk-icon-times"></i> Cancel</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>

{% endblock%}
